﻿using OrderControl;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderControlWinForm {
    public partial class Form_Display : Form {
        public Form_Display() {
            InitializeComponent();
        }

        private void Form_Display_Load(object sender, EventArgs e) {
            bindingSource_Order.DataSource = OrderService.GetAllOrders();
            toolStripComboBox_SearchType.SelectedIndex = 0;
        }

        private void dataGridView_Order_RowEnter(object sender, DataGridViewCellEventArgs e) {
            bindingSource_OrderDetail.DataSource = bindingSource_Order[e.RowIndex];
        }

        private void ToolStripMenuItem_Import_Click(object sender, EventArgs e) {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "xml files (*.xml)|*.xml";
            fileDialog.RestoreDirectory = true;
            if (fileDialog.ShowDialog() == DialogResult.OK) {
                OrderService.Import(fileDialog.FileName);
                bindingSource_Order.DataSource = OrderService.GetAllOrders();
                if (OrderService.GetAllOrders().Any()) {
                    bindingSource_OrderDetail.DataSource = bindingSource_Order[0];
                }
            }
        }

        private void ToolStripMenuItem_Export_Click(object sender, EventArgs e) {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "xml files (*.xml)|*.xml";
            fileDialog.RestoreDirectory = true;
            if (fileDialog.ShowDialog() == DialogResult.OK) {
                OrderService.Export(fileDialog.FileName);
            }
        }

        private void toolStripButton_Add_Click(object sender, EventArgs e) {
            Order order;
            while (true) {
                Form_Order form_Order = new Form_Order();
                if (form_Order.ShowDialog() == DialogResult.OK) {
                    order = form_Order.UsingOrder;
                    OrderService.AddOrder(order);
                }
                break;
            }
            bindingSource_Order.DataSource = OrderService.GetAllOrders();
        }

        private void toolStripButton_Delete_Click(object sender, EventArgs e) {
            OrderService.DeleteOrder((int)dataGridView_Order[0, dataGridView_Order.CurrentCell.RowIndex].Value);
            bindingSource_Order.DataSource = OrderService.GetAllOrders();
        }

        private void toolStripButton_Edit_Click(object sender, EventArgs e) {
            Order order = OrderService.SearchById((int)dataGridView_Order[0, dataGridView_Order.CurrentCell.RowIndex].Value);
            OrderService.DeleteOrder(order.OrderId);
            bindingSource_Order.ResetBindings(false);
            Form_Order form_Order = new Form_Order(order);
            form_Order.ShowDialog();
            order = form_Order.UsingOrder;
            OrderService.AddOrder(order);
            bindingSource_Order.DataSource = OrderService.GetAllOrders();
        }

        private void toolStripButton_Search_Click(object sender, EventArgs e) {
            bool isRightFormat = true;
            string inputText = toolStripTextBox_SearchInput.Text;
            switch (toolStripComboBox_SearchType.SelectedIndex) {
                case 0:
                    if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+$")) {
                        bindingSource_Order.DataSource = OrderService.SearchById(int.Parse(inputText));
                    }
                    break;
                case 1:
                    bindingSource_Order.DataSource = OrderService.SearchByCommodity(inputText);
                    break;
                case 2:
                    bindingSource_Order.DataSource = OrderService.SearchByCustomer(inputText);
                    break;
                case 3:
                    if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+([.][0-9]*)?$")) {
                        bindingSource_Order.DataSource = OrderService.SearchByPriceHigher(double.Parse(inputText));
                    }
                    break;
                case 4:
                    if (isRightFormat = Regex.IsMatch(inputText, "^[0-9]+([.][0-9]*)?$")) {
                        bindingSource_Order.DataSource = OrderService.SearchByPriceLower(double.Parse(inputText));
                    }
                    break;
            }
            if (!isRightFormat) {
                if (MessageBox.Show("Error format!", "Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) {
                    toolStripTextBox_SearchInput.Text = "";
                }
            } else {
                toolStripButton_Reset.Enabled = true;
            }
        }

        private void toolStripButton_Reset_Click(object sender, EventArgs e) {
            bindingSource_Order.DataSource = OrderService.GetAllOrders();
            toolStripButton_Reset.Enabled = false;
        }
    }
}
